<?php
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/9/27 0027
 * Time: 14:01
 */

namespace Wx\Controller;

use Think\Controller;

class IndexController extends CommonController
{
    //首页
    public function index(){
        //用户信息
        $member = getMember(session('u_id'));//获取用户信息
        if($member['status']==1){
            $this->error("该账号已被禁用！");die;
        }//end if
        $store = M("shop_store")->where("c_id = {$member['c_id']} AND status = 1 ")->Field('id,storename')->find();
        $store['community']= M("shop_community")->where("id = {$member['c_id']}")->getField('title');//获取小区地址

        //积分兑换-start
        if($store) {
            //社区店铺兑换
            $exchange_goods = D('exchange_goods');
            $shop_store = D('shop_store');
            $s_u_id = $shop_store->where("id=" . $store['id'])->getField('u_id');//社区店主

            $goods = $exchange_goods->where("state = 1")->select();//兑换产品
            foreach($goods as $k=>$v){
                $c_id_str = explode(',',$v['c_id_str']);
                if($v['u_id']==$s_u_id || in_array($member['c_id'],$c_id_str)){

                }else{
                    unset($goods[$k]);
                };
            }//end if
//            dump($goods);die;
            $goods_1 = '';//纯积分
            $goods_2 = '';//积分+现金
            $goods_3 = '';//纯现金
            $category =M('category');
//            dump($goods);die;
            foreach ($goods as $i => $one) {
                //u_id=0的判断本店是否在授权范围内
                $c_id_arr = explode(',', $one['c_id_str']);
                if (($one['u_id'] == 0 && in_array($member['c_id'], $c_id_arr)) || $one['u_id'] > 0) {
                    //本小区在该品牌兑换内|兑换是本社区发布
                } else {
                    continue;
                }//end if
                if ($one['state'] != 1) continue;//只展示上架产品
                //产品第一图片
                $img = explode(',', $one['head_img']);
                $goods[$i]['f_img'] = $img[0];//第一张图片作为封面图
                //查商品单位
                $getc = $category->where("id=" . $one['unit'])->find();
                $goods[$i]['unit'] = $getc['title'];
                if ($one['level_cash_1'] == 0 and $one['level_cash_2'] == 0 and $one['level_cash_3'] == 0 and $one['level_cash_4'] == 0) {
                    $goods_1[] = $goods[$i];
                } else if ($one['level_integral_1'] == 0 and $one['level_integral_2'] == 0 and $one['level_integral_3'] == 0 and $one['level_integral_4'] == 0) {
                    $goods_3[] = $goods[$i];
                } else {
                    $goods_2[] = $goods[$i];
                }//end if

            }//end foreach
            $this->assign('goods_integral',$goods_1);
            $this->assign('goods_int_ca',$goods_2);
            $this->assign('goods_cash',$goods_3);
        }//end if
        //积分兑换-end

        //拼团商品-start
        $shop_spellgroup=D('shop_spellgroup');//拼团表
        $shop_spellgroup_copy=D('shop_spellgroup_copy');//小区副本

        //乡村拼团————————————————————————start
        //即将成团（本小区）————————————————————————————————start
        if($member["c_id"]>0){
            $getneedgood=$shop_spellgroup_copy->where("orsuccess=0 and state=0 and c_id=".$member["c_id"])->limit(4)->select();//dump($getneedgood);die;
            foreach($getneedgood as $k=>$one){
                $getgood=$shop_spellgroup->where("id=".$one['sg_id']." AND state=1")->find();

                //首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名、已售多少份、还差多少成团
                if($getgood && $getgood['type'] == 0){
                    //产品第一图片
                    $img=explode(',',$getgood['h_imgs']);
                    $getneedgood[$k]['f_img']=$img[0];//第一张图片作为封面图
                    $getneedgood[$k]['type']=$getgood['type'];
                    $getneedgood[$k]['title']=$getgood['title'];
                    $getneedgood[$k]['u_num']=$getgood['u_num'];
                    $getu=$category->where("id=".$getgood['unit'])->find();

                    $getneedgood[$k]['unit']=$getu['title'];
                    $getneedgood[$k]['actual_price']=$getgood['actual_price'];
                    //发布人头像、姓名
                    $shop_spellgroup=D('shop_spellgroup');
                    $getsg=$shop_spellgroup->where("id=".$one['sg_id'])->find();
                    $getinfo=M("member")->where("id=".$getsg['u_id'])->find();
                    $getneedgood[$k]['f_realname']=$getinfo['realname'];
                    $getneedgood[$k]['f_avatar']=$getinfo['avatar'];

                    $getneedgood[$k]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['sg_id'])->sum('num');//已售多少份
                    $getneedgood[$k]['neednum']=$getgood['min_num']-$one['num'];
                }else{
                    unset($getneedgood[$k]);
                }//end if
            }//end foreach
            $this->assign('needgoodarr',$getneedgood);
        }//end if
        //即将成团（本小区）————————————————————————————————end
//        dump(2);die;

        //新上架产品——————————————————————————————————start
        $newshoparr=$shop_spellgroup->where("state=1 AND type=0 and (".time().">starttime and ".time()."<endtime)")->order("starttime asc")->limit(4)->select(); // 查询第一页数据
//        dump($newshoparr);die;
        //查询当前用户小区id
        if(session('u_id')>0) {

            foreach($newshoparr as $i=>$one){
                if($member["c_id"]>0){
                    //已经绑定小区
                    //用户本小区拼团信息
                    $getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$member["c_id"])->find();//用户本小区该商品是否开团
                    if($getcopy){
                        $newshoparr[$i]['havecopy']=1;//表示已经开团
                        if($one['min_num']- $getcopy['num']>0){
                            $newshoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
                        }else{
                            $newshoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
                        }//end if

                    }else{
                        $newshoparr[$i]['havecopy']=0;//未开团

                    }//end if
                }else{
                    //未绑定小区
                    $newshoparr[$i]['havecopy']=0;//未开团
                }//end if
                //产品第一图片
                $img=explode(',',$one['h_imgs']);
                $newshoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
                //发布人头像、姓名
                if($one['u_id']>0)$getinfo=M("member")->where("id=".$one['u_id'])->find();
                $newshoparr[$i]['f_realname']=$getinfo['realname'];
                $newshoparr[$i]['f_avatar']=$getinfo['avatar'];
                $newshoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份

            }//end foreach
        }//endif
        $this->assign('newshoparr',$newshoparr);
        //新上架产品——————————————————————————————————end


        //即将上架————————————————————————————start
        $willgood=$shop_spellgroup->where("state=1 AND type=0 and starttime>".time())->order("starttime asc")->limit(4)->select(); // 查询第一页数据
        foreach($willgood as $k=>$one){
            $getgood=$shop_spellgroup->where("id=".$one['id'])->find();
            //首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名
            //产品第一图片
            $img=explode(',',$getgood['h_imgs']);
            $willgood[$k]['f_img']=$img[0];//第一张图片作为封面图
            $willgood[$k]['type']=$getgood['type'];
            $willgood[$k]['title']=$getgood['title'];
            $willgood[$k]['u_num']=$getgood['u_num'];
            $getu=$category->where("id=".$getgood['unit'])->find();
            $willgood[$k]['unit']=$getu['title'];
            $willgood[$k]['actual_price']=$getgood['actual_price'];
            //发布人头像、姓名
            if($one['u_id']>0)$getinfo=M("member")->where("id=".$one['u_id'])->find();
            $willgood[$k]['f_realname']=$getinfo['realname'];
            $willgood[$k]['f_avatar']=$getinfo['avatar'];
            //上架时间、关注人数
            $willgood[$k]['starttime']=$getgood['starttime'];
        }//end foreach
        $this->assign('willgood',$willgood);
        //即将上架————————————————————————————end

        //乡村拼团————————————————————————————end

        //门店拼团————————————————————————————start

        //即将成团（本小区）————————————————————————————————start
        if($member["c_id"]>0){
            $getneedgood=$shop_spellgroup_copy->where("orsuccess=0 and state=0 and c_id=".$member["c_id"])->limit(4)->select();//dump($getneedgood);die;
            foreach($getneedgood as $k=>$one){
                $getgood=$shop_spellgroup->where("id=".$one['sg_id']." AND state=1")->find();
                //首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名、已售多少份、还差多少成团
                if($getgood && $getgood['type'] == 1){
                    //产品第一图片
                    $img=explode(',',$getgood['h_imgs']);
                    $getneedgood[$k]['f_img']=$img[0];//第一张图片作为封面图
                    $getneedgood[$k]['type']=$getgood['type'];
                    $getneedgood[$k]['title']=$getgood['title'];
                    $getneedgood[$k]['u_num']=$getgood['u_num'];
                    $getu=$category->where("id=".$getgood['unit'])->find();
                    $getneedgood[$k]['unit']=$getu['title'];
                    $getneedgood[$k]['actual_price']=$getgood['actual_price'];
                    //发布人头像、姓名
                    $shop_spellgroup=D('shop_spellgroup');
                    $getsg=$shop_spellgroup->where("id=".$one['sg_id'])->find();
                    $getinfo=M("member")->where("id=".$getsg['u_id'])->find();
                    $getneedgood[$k]['f_realname']=$getinfo['realname'];
                    $getneedgood[$k]['f_avatar']=$getinfo['avatar'];

                    $getneedgood[$k]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['sg_id'])->sum('num');//已售多少份
                    $getneedgood[$k]['neednum']=$getgood['min_num']-$one['num'];
                }else{
                    unset($getneedgood[$k]);
                }//end if

            }//end foreach
            $this->assign('needgood_farmers',$getneedgood);
        }//end if
        //即将成团（本小区）————————————————————————————————end

        //新上架产品——————————————————————————————————start
        $newshoparr=$shop_spellgroup->where("state=1 AND type=1 and (".time().">starttime and ".time()."<endtime)")->order("starttime asc")->limit(4)->select(); // 查询第一页数据
//        dump($newshoparr);die;
        //查询当前用户小区id
        if(session('u_id')>0) {

            foreach($newshoparr as $i=>$one){
                if($member["c_id"]>0){
                    //已经绑定小区
                    //用户本小区拼团信息
                    $getcopy=$shop_spellgroup_copy->where("sg_id=".$one['id']." and c_id=".$member["c_id"])->find();//用户本小区该商品是否开团
                    if($getcopy){
                        $newshoparr[$i]['havecopy']=1;//表示已经开团
                        if($one['min_num']- $getcopy['num']>0){
                            $newshoparr[$i]['neednum']=$one['min_num']- $getcopy['num'];//用户本小区还差几人份成团
                        }else{
                            $newshoparr[$i]['neednum']=0;//拼团成功，还可以继续参团
                        }//end if

                    }else{
                        $newshoparr[$i]['havecopy']=0;//未开团

                    }//end if
                }else{
                    //未绑定小区
                    $newshoparr[$i]['havecopy']=0;//未开团
                }//end if
                //产品第一图片
                $img=explode(',',$one['h_imgs']);
                $newshoparr[$i]['f_img']=$img[0];//第一张图片作为封面图
                //发布人头像、姓名
                if($one['u_id']>0)$getinfo=M("member")->where("id=".$one['u_id'])->find();
                $newshoparr[$i]['f_realname']=$getinfo['realname'];
                $newshoparr[$i]['f_avatar']=$getinfo['avatar'];
                $newshoparr[$i]['actual_num']=$shop_spellgroup_copy->where("sg_id=".$one['id'])->sum('num');//已售多少份

            }//end foreach
        }//endif
        $this->assign('newshoparr_farmers',$newshoparr);
        //新上架产品——————————————————————————————————end

        //即将上架————————————————————————————start
        $willgood=$shop_spellgroup->where("state=1 AND type=1 and starttime>".time())->order("starttime asc")->limit(4)->select(); // 查询第一页数据
        foreach($willgood as $k=>$one){
            $getgood=$shop_spellgroup->where("id=".$one['id'])->find();
            //首页图、类型、标题、规格、拼团价、sg_id、发布人头像姓名
            //产品第一图片
            $img=explode(',',$getgood['h_imgs']);
            $willgood[$k]['f_img']=$img[0];//第一张图片作为封面图
            $willgood[$k]['type']=$getgood['type'];
            $willgood[$k]['title']=$getgood['title'];
            $willgood[$k]['u_num']=$getgood['u_num'];
            $getu=$category->where("id=".$getgood['unit'])->find();
            $willgood[$k]['unit']=$getu['title'];
            $willgood[$k]['actual_price']=$getgood['actual_price'];
            //发布人头像、姓名
            if($one['u_id']>0)$getinfo=M("member")->where("id=".$one['u_id'])->find();
            $willgood[$k]['f_realname']=$getinfo['realname'];
            $willgood[$k]['f_avatar']=$getinfo['avatar'];
            //上架时间、关注人数
            $willgood[$k]['starttime']=$getgood['starttime'];
        }//end foreach

        $this->assign('willgood_farmers',$willgood);
        //即将上架————————————————————————————end


        //门店拼团————————————————————————————end
        //拼团商品-end
        $this->assign("store",$store);
        $this->display("index");
    }//end fun

    //小区选择页
    public function change(){
        $id = session('u_id');
        $member = M("member")->where("id = {$id}")->find();//获取用户信息

        if(IS_POST){
            $Post_datekey =  explode('-',$_POST['datekey']);

            $datekey = array(
                'a_id_1' => $Post_datekey[0],
                'a_id_2' => $Post_datekey[1],
                'a_id_3' => $Post_datekey[2]
            );
//            $c_info = M('shop_community')->where(array('a_id_1' => $datekey[0],'a_id_2' => $datekey[1],'a_id_3' => $datekey[2] ))->select();
        }else{
            // 省市区信息+小区信息
            if($member['c_id']>0){
                $datekey = M('shop_community')->where(array('id' => $member['c_id'] ))->field('a_id_1,a_id_2,a_id_3')->find();//用户小区
//                $c_info = M('shop_community')->where(array('a_id_1' => $datekey["a_id_1"],'a_id_2' => $datekey["a_id_2"],'a_id_3' => $datekey["a_id_3"] ))->select();
            }//end if
        }//end if

        if($datekey) {
            $c_info = M('shop_community')->where(array('a_id_1' => $datekey["a_id_1"], 'a_id_2' => $datekey["a_id_2"], 'a_id_3' => $datekey["a_id_3"]))->select();//小区信息
            foreach($c_info as $k => $v){
                $c_info[$k]['storename']=M("shop_store")->where("c_id = {$v['id']} AND status = 1")->getField('storename');//店铺名称
                if($c_info[$k]['storename']==''){
                    //没有店铺的小区不显示
                    unset($c_info[$k]);
                }//end if
            }//end foreach
        }//end if

        //地址js生成
        $ali=set_area_js();
        $this->assign('ali',$ali);
        //地址js生成-end

        $this->assign('datekey',$datekey);
        $this->assign('c_info',$c_info);
        $this->display("store_change");
//        dump($c_info);
    }//end fun

    //修改地址
    public function EditCommunity(){
        $result = M("member")->where("id = ".session('u_id'))->save(['c_id'=>$_POST['c_id']]);
        if($result){
            $this->ajaxReturn(['state'=>0,'val'=>'小区更改成功！','url'=>"Index/index"],"JSON");
        }else{
            $this->ajaxReturn(['state'=>1,'val'=>'未知错误！'],"JSON");
        }//end if
    }//end fun

}//end class